<template>
  <div>
    <el-dialog
      class="base-dialog"
      :class="{ readonly: readOnly }"
      :custom-class="'panna-dialog_medium'"
      :title="title"
      align="left"
      :append-to-body="true"
      :visible.sync="innerDialogFormVisible"
      :before-close="handleClose"
    >
      <p slot="title">{{ title }}</p>
      <el-table
        ref="table"
        v-loading="tableLoad"
        :data="tableData"
        style="width: 100%"
        height="400px"
      >
        <el-table-column prop="sampleNum" label="样品编号"></el-table-column>
        <el-table-column prop="sampleName" label="样品名称"></el-table-column>
        <el-table-column prop="sampleModel" label="产品型号"></el-table-column>
        <el-table-column prop="detectionItemName" label="检测项目" />
        <el-table-column prop="sampleStoreCount" label="留存数量">
          <template slot-scope="scope">
            <el-input v-model="scope.row.sampleStoreCount"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="入库日期" width="150px">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.sampleStoreDate"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </template>
        </el-table-column>
        <el-table-column label="保存日期" width="150px">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.saveDate"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </template>
        </el-table-column>
      </el-table>
      <span v-if="!readOnly" slot="footer" class="base-dialog-footer">
        <el-button
          type="primary"
          :disabled="!submitReadOnly"
          @click="onSubmit('formData')"
          >保 存</el-button
        >
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { create } from "../../../api/SampleManage/SampleStore";
import _ from "lodash";

export default {
  name: "SampleStoreDialog",
  components: {},
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    editId: {
      type: String,
      default: "",
    },
    readOnly: {
      type: Boolean,
      default: false,
    },
    // 接收样品数据
    rowData: {
      type: Array,
      default: function() {
        return [];
      },
    },
  },
  data() {
    return {
      title: "",
      tableLoad: false,
      tableData: [],
      submitLoad: false,
      submitReadOnly: true,
      innerDialogFormVisible: false,
      userDialogFormVisible: false,
      chioseUserIndex: 0,
    };
  },
  watch: {
    rowData(val) {
      this.tableData = val;
    },
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
      this.title = this.readOnly
        ? "查看样品留存"
        : this.editId
          ? "查看样品留存"
          : "样品留存";
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  created() {},
  methods: {
    // 判断输入内容
    blurTexts(e, row) {
      const boolean = new RegExp("^[1-9][0-9]*$").test(e.target.value);
      if (!boolean) {
        this.$message.warning("请输入正整数");
        e.target.value = "";
        return false;
      }
      if (e.target.value > row.sampleCount) {
        this.$message.warning("领取数量不能大于接收数量");
        e.target.value = "";
        return false;
      }
    },
    // 提交
    onSubmit: _.throttle(function() {
      if (this.submitReadOnly) {
        this.submitReadOnly = false;
        var data = [];
        for (let i = 0; i < this.tableData.length; i++) {
          data.push({
            entrustOrderId: this.tableData[i].entrustOrderId,
            sysBusinessSampleInfoId: this.tableData[i].sysBusinessSampleInfoId,
            sampleStoreCount: this.tableData[i].sampleStoreCount,
            sampleStoreDate: this.tableData[i].sampleStoreDate,
            saveDate: this.tableData[i].saveDate,
          });
        }

        if (!this.editId) {
          this.addAPI(data);
        } else {
          this.editAPI();
        }
        return false;
      }
    }, 2000),
    // 新增
    addAPI(data) {
      create(data)
        .then((res) => {
          this.submitReadOnly = true;
          this.handleClose();
        })
        .catch(() => {
          this.submitReadOnly = true;
        });
    },
    // 关闭弹框
    handleClose() {
      this.closeDialog();
    },
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      // 清空数据
      this.$emit("closeSampleReceivingDialog");
      this.tableData = [];
      this.cancel();
    },
  },
};
</script>
